﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Devices.Sensors;

using SHWARSICO.Gaming.XNASter._2D;

namespace HoleGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class HoleGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GObject hole, ball;
        SpriteFont font;
        Random Rand = new Random();
        int counter = 0;
        Accelerometer accel = new Accelerometer();
        int score = 0;


        public HoleGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.SupportedOrientations = DisplayOrientation.Portrait;
            graphics.PreferredBackBufferHeight = 800;
            graphics.PreferredBackBufferWidth = 480;

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Extend battery life under lock.
            InactiveSleepTime = TimeSpan.FromSeconds(1);

            hole = new GObject(this, "Hole");
            ball = new GObject(this, "Ball");
            accel.Start();

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            GObject.LoadContentAll();
            font = Content.Load<SpriteFont>("Font");
            ball.Randomize(Rand, 10);
            ball.Friction = 0.9f;

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (counter == 0)
            {
                counter = Rand.Next(50, 250);
                hole.Randomize(Rand, 20);
            }
            counter--;

            var R = accel.CurrentValue;
            ball.Accel = new Vector2(R.Acceleration.X * 1.5f, -R.Acceleration.Y * 1.5f);

            GObject.UpdateAll();

            if (GObject.Close(ball, hole, 50))
            {
                counter = Rand.Next(50, 250);
                hole.Randomize(Rand, 20);
                score++;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.DarkGreen);

            spriteBatch.Begin();
            GObject.DrawAll(spriteBatch);
            spriteBatch.DrawString(font, score.ToString(), Vector2.Zero, Color.Yellow);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
